// pages/mine/mine.js
const api = require("../../comm/api")
const http = require("../../utils/http")
const util = require("../../utils/util")
import Notify from '../../components/@vant/weapp/notify/notify';
import Dialog from '../../components/@vant/weapp/dialog/dialog';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        user: {},
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        const token = util.readToken()
        if (!!token) {
            this.loadUserInfo()
        } else {
            this.promotLogin('您尚未登录，现在登录吗？')
        }
    },

    loadUserInfo() {
        http.getJSONWithToken(api.User.Info.PATH, pojo => {
            const user = pojo.data
            this.setData({
                user: user
            })
            console.log(this.data.user)
        }, err => {
            if (err.code === 401) {
                this.promotLogin('登录超时，现在重新登录吗？')
            } else {
                Notify({
                    type: 'warning',
                    message: JSON.stringify(err)
                })
            }
        })
    },

    promotLogin(msg) {
        Dialog.confirm({
            title: '信息提示',
            message: msg
        }).then(() => {
            wx.reLaunch({
                url: '/pages/login/login',
            })
        }).catch(() => {
            wx.switchTab({
                url: '/pages/home/home',
            })
        })
    },

    logout() {
        Dialog.confirm({
            title: '信息提示',
            message: '您确定要退出当前登录状态吗？'
        }).then(() => {
            util.clean()
            this.setData({
                user: {}
            })
            wx.switchTab({
                url: '/pages/home/home',
            })
        })
    }
})